package cn.iocoder.yudao.module.pypm.controller.admin.projectstage;

import org.springframework.web.bind.annotation.*;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import jakarta.validation.*;
import jakarta.validation.*;
import jakarta.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.pypm.controller.admin.projectstage.vo.*;
import cn.iocoder.yudao.module.pypm.dal.dataobject.projectstage.ProjectStageDO;
import cn.iocoder.yudao.module.pypm.service.projectstage.ProjectStageService;

@Tag(name = "管理后台 - 项目阶段")
@RestController
@RequestMapping("/pypm/project-stage")
@Validated
public class ProjectStageController {

    @Resource
    private ProjectStageService projectStageService;

    @PostMapping("/create")
    @Operation(summary = "创建项目阶段")
    @PreAuthorize("@ss.hasPermission('pypm:project-stage:create')")
    public CommonResult<Long> createProjectStage(@Valid @RequestBody ProjectStageSaveReqVO createReqVO) {
        return success(projectStageService.createProjectStage(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新项目阶段")
    @PreAuthorize("@ss.hasPermission('pypm:project-stage:update')")
    public CommonResult<Boolean> updateProjectStage(@Valid @RequestBody ProjectStageSaveReqVO updateReqVO) {
        projectStageService.updateProjectStage(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除项目阶段")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('pypm:project-stage:delete')")
    public CommonResult<Boolean> deleteProjectStage(@RequestParam("id") Long id) {
        projectStageService.deleteProjectStage(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得项目阶段")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('pypm:project-stage:query')")
    public CommonResult<ProjectStageRespVO> getProjectStage(@RequestParam("id") Long id) {
        ProjectStageDO projectStage = projectStageService.getProjectStage(id);
        return success(BeanUtils.toBean(projectStage, ProjectStageRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得项目阶段分页")
    @PreAuthorize("@ss.hasPermission('pypm:project-stage:query')")
    public CommonResult<PageResult<ProjectStageRespVO>> getProjectStagePage(@Valid ProjectStagePageReqVO pageReqVO) {
        PageResult<ProjectStageDO> pageResult = projectStageService.getProjectStagePage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, ProjectStageRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出项目阶段 Excel")
    @PreAuthorize("@ss.hasPermission('pypm:project-stage:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportProjectStageExcel(@Valid ProjectStagePageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<ProjectStageDO> list = projectStageService.getProjectStagePage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "项目阶段.xls", "数据", ProjectStageRespVO.class,
                        BeanUtils.toBean(list, ProjectStageRespVO.class));
    }

}